package com.cq.oa.web.wfc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.util.Page;
import oa.base.web.Action.BaseAction;

import com.cq.oa.domain.dto.WorkDto;
import com.cq.oa.domain.wfc.ProcessDefinition;
import com.cq.oa.domain.wfc.ProcessType;
import com.cq.oa.service.work.ProcessDefinitionService;

public class WfcProcessAction extends BaseAction<ProcessDefinition>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2283944864146115800L;
	
	private WorkDto dto;
	
	private Long types;
	
	

	public Long getTypes() {
		return types;
	}


	public void setTypes(Long types) {
		this.types = types;
	}


	public WorkDto getDto() {
		return dto;
	}


	public void setDto(WorkDto dto) {
		this.dto = dto;
	}


	/* (non-Javadoc)
	 * @see oa.base.web.Action.BaseAction#getEntityClass()
	 */
	@Override
	protected Class<ProcessDefinition> getEntityClass() {
		return ProcessDefinition.class;
	}
	
	private ProcessDefinitionService processDefinitionService;

	public void setProcessDefinitionService(
			ProcessDefinitionService processDefinitionService) {
		this.processDefinitionService = processDefinitionService;
	}
	
	
	public String findByPage() throws Exception {
		Page<ProcessDefinition> users = processDefinitionService.findByPage(filterMap,linkMap,shortMap, super.getPageNo(), super.getPageSize());
		this.renderJsonSuccess(jsonUtils.getExtJson(users, new String[] {}));
		return JSON;
	}
	
	public String operate(){
		initLoad();
		if(null!=entity.getSid()){
			entity = processDefinitionService.findByParmkeys(entity.getSid());
		}
		return "operate";
	}
	
	public String save(){
		try {
			processDefinitionService.doProcessDefinition(entity);
		} catch (Exception e) {
			this.renderJsonError(e);
		}
		this.renderJsonSuccess();
		return JSON;
	}
	
	public void initLoad(){
		Map<String,Object> params = new HashMap<String, Object>();
		List<ProcessType> processTypes = this.processDefinitionService.getProcessTypeDao().find(params);
		this.putKey.put("processTypes", processTypes);
	}
	
	/**
	 * 保存节点属性
	 * @return
	 * lilang 
	 * 2013-10-25下午12:38:54
	 */
	public String saveDefinition(){
		try{
			this.processDefinitionService.doActivityDefinition(dto);
			this.renderJsonSuccess();
		}catch(Exception e){
			this.renderJsonError(e);
		}
		return JSON;
	}
	
	public String delete(){
		this.renderJsonSuccess();
		return JSON;
	}
	
	public String loadDefinition() throws Exception{
		dto = this.processDefinitionService.findProcessDefinitionById(entity.getSid());
		if(null!=types){
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("processTypeSid", types);
			dto.setProcessJsps(this.processDefinitionService.getProcessJspDao().find(params));
		}
		this.renderJsonSuccess(jsonUtils.getExtJson(dto, new String[]{}));
		return JSON;
	}
	
	public String draw(){
		
		return "draw";
	}
	
	
	
}
